package io.itit.ecp.admin.provide.action.resp.largescreen;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;


@Data
public class EnergySubStatisticsResp implements Serializable {

    @ApiModelProperty("总量")
    private BigDecimal total;

    @ApiModelProperty("单位")
    private String unit;

    @ApiModelProperty("数据详情")
    private List<Detail> data;

    public BigDecimal getTotal() {
        if (Objects.isNull(total)) {
            if (this.data == null) {
                this.total = BigDecimal.ZERO;
            } else {
                this.total = this.data.stream()
                        .map(Detail::getValue)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_DOWN);
            }
        }
        return total;
    }

    @Data
    @Accessors(chain = true)
    public static class Detail implements Serializable {

        @ApiModelProperty("名称")
        private String name;

        @ApiModelProperty("统计值")
        private BigDecimal value;

    }
}
